<?php
namespace TestAdmin\Controller;
use Think\Controller;

use Common\Controller\CommonActions\OrgnizerActionController as OrgC;
class ManageQuizController extends BaseController {


    public function content()
    {
        //显示内置问卷
        $bulidin = M("quiz_info")->where("quiz_id <= 12")->select();

        foreach($bulidin as &$b){
            $b['add_time'] = convertUnixTime($b['add_time']);
        }

        $custom = M("quiz_info")->where("quiz_id > 12")->select();

        foreach($custom as &$c){
            $c['add_time'] = convertUnixTime($c['add_time']);
        }
        $this->assign("bulidin_quiz", $bulidin);
        $this->assign("custom_quiz", $custom);
        $this->display();
    }

    public function deleteQuiz($quiz_id)
    {


        M("quiz_info")->where("quiz_id = $quiz_id")->delete();
        M("question")->where("quiz_id = $quiz_id")->delete();
        M("element_info")->where("quiz_id = $quiz_id")->delete();
        M("element_extend")->where("quiz_id = $quiz_id")->delete();
        M("element_desc")->where("quiz_id = $quiz_id")->delete();
        M("calc_result")->where("quiz_id = $quiz_id")->delete();
        M("user_answer")->where("quiz_id = $quiz_id")->delete();




        echo 'delete succeed!';
       // $this->ajaxReturn(['msg' => '删除成功！']);
    }



    public function setQuizType()
    {
        $quiz_type = I("quiz_type");
        session("quiz_type", $quiz_type);
        $this->ajaxReturn(['msg' => 'setType success']);
    }


    public function testUpload()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xlsx', 'xls');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息

            $rs['msg'] = $upload->getError();
            $rs['code'] = -2;

        }else{// 上传成功

            $filepath = $_SERVER['DOCUMENT_ROOT'].'/Uploads/'.$info['file']['savepath'].$info['file']['savename'];

            $url = "http://localhost:8090/demo/Main";
            $data['action']='quizcheck';
            $data['filepath'] = $filepath;
            $data['sourcename'] = "check";
            $data["type"] = session("quiz_type");

            $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用问卷部分java

            if (!$httpstr){
                $rs['msg'] = "问卷名解析错误，检查提交问卷格式！";
                $rs['code'] = 0;
            }
            else{
                switch($httpstr)
                {
                    case 2:
                        $rs['msg'] = "上传成功！";
                        $rs['code'] = 2;
                        $rs['log'] = $httpstr;
                        break;
                    case -1:
                        $rs['msg'] = "问卷名和已有问卷重复！";
                        $rs['code'] = 1;

                        break;
                    default:
                        //暂存文件路径
                        session("tspath", $filepath);
                        $rs['msg'] = "返回内容";
                        $rs['code'] = 1;
                        $rs['log'] = $httpstr;
                        break;
                }
            }

        }
        $this->ajaxReturn($rs);

    }

    public function testsourceSave()
    {
        $filepath = session("tspath");

        $url = "http://localhost:8090/demo/Main";
        $data['action']='savequiz';
        $data['filepath'] = $filepath;
        $data['sourcename'] = I("sourcename");
        $data["type"] = session("quiz_type");

        if (!$data['sourcename']){
            $rs['msg'] = "问卷名不能为空！";
            $rs['code'] = -1;
            $this->ajaxReturn($rs);
            exit;
        }


        $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用问卷部分java

        if ($httpstr == 1){

            $quiz_info['quiz_pagetitle'] = I("pagetitle_input");
            $quiz_info['quiz_pageintroduce'] = I("quiz_introduce");
            $quiz_info['quiz_pagetips'] = I("quiz_tips");


            $dao = M("quiz_info");
            $dao->where(['quiz_id' => $dao->max('quiz_id')])->save($quiz_info);


            $rs['msg'] = "保存成功！";
            $rs['code'] = 1;
        }
        else if ($httpstr == -1){
            $rs['msg'] = "问卷重复！";
            $rs['code'] = -1;
        }
        else{
            $rs['msg'] = "发生错误！";
            $rs['code'] = 0;
        }

        $this->ajaxReturn($rs);

    }
}